<template>
  <div>
    <e-table
      ref="eTable"
      @getList="handleGetList"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
      :total="total"
    >
      <el-table-column show-overflow-tooltip slot="name" label="方案名称" align="center">
        <span slot-scope="scope">
          <el-button
            class="fl"
            type="text"
            @click="getDetail(scope.row)"
          >{{ scope.row.evalSchemeName }}</el-button>
        </span>
      </el-table-column>
      <el-table-column slot="owner" label="私有" width="60" align="center">
        <span slot-scope="scope">{{ scope.row.ownerUserId ? '是' : '否' }}</span>
      </el-table-column>
      <el-table-column slot="action" fixed="right" label="操作" width="50" align="center">
        <template slot-scope="scope">
          <el-dropdown>
            <a class="el-dropdown-link">
              <i class="el-icon-menu"></i>
            </a>
            <el-dropdown-menu slot="dropdown">
              <!-- <el-dropdown-item
                @click.native="handleEdit('detail', scope.row)"
                v-if="$auth('eval:detail')"
              >
                <a>详情</a>
              </el-dropdown-item>-->
              <el-dropdown-item
                @click.native="handleEdit('edit', scope.row)"
                v-if="$auth('eval:update')"
              >
                <a href="javascript:;">编辑</a>
              </el-dropdown-item>
              <el-dropdown-item
                v-if="$auth('eval:add')"
                @click.native="handleEdit('copyAdd', scope.row)"
              >
                <a href="javascript:;">复制新增</a>
              </el-dropdown-item>
              <el-dropdown-item v-if="$auth('eval:delete')" @click.native="handleDelete(scope.row)">
                <a class="red" href="javascript:;">删除</a>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </e-table>
  </div>
</template>

<script>
import { schemeQuery, schemeDelete, schemeCreate } from '@/api/eval'
import { formatTime } from '@/utils/util'

import { openNotification } from '@/utils/util'
export default {
  data() {
    return {
      loading: false,
      tableData: [],
      total: 1,
      columns: [
        { slot: 'name' },
        {
          label: '创建人',
          width: '100px',
          prop: 'createUser'
        },
        {
          label: '创建时间',
          prop: 'editorTime',
          formatter(row, column, cellValue, index) {
            return formatTime(row.editorTime)
          }
        },
        { slot: 'owner' },
        { slot: 'action' }
      ],
      queryParam: {
        pageNo: 1,
        pageSize: 10
      }
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    handleEdit(type, row) {
      console.log(type, row)
      this.$emit('edit', type, row)
    },
    async handleGetList(param = {}) {
      this.loading = true
      const res = await schemeQuery({ ...this.queryParam, ...param })
      if (res.data && res.data.records.length > 0) {
        const {
          state,
          data: { records, total }
        } = res
        this.tableData = records
        this.total = total
      } else {
        this.tableData = []
        this.total = 1
      }
      this.loading = false
    },
    handleDelete(row) {
      console.log('row', row)

      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.evalSchemeName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await schemeDelete(row.evalSchemeId, {})
          openNotification('success', '删除成功')
          _this.handleGetList()
        }
      })
    },
    async handleAdd(row) {
      row.evalSchemeId = null
      const res = await schemeCreate(row)

      console.log('res', res)
    },
    getDetail(row) {
      this.$emit('detail', row)
    }
  }
}
</script>